//index.js
var WxSearch = require('../../wxSearch/wxSearch.js')

//获取应用实例
var app = getApp()
var _openid = app.getOpenId();
Page({
  data: {
    sercherStorage: [], //
    inputValue: "",    //搜索框输入的值  
    StorageFlag: false, //显示搜索记录标志位
    goods:[],
    loadingMoreHidden:true,
    coupons: [],
    nearLatitude:'',//经度
    nearLongitude:'', //纬度
    goodsDetail:{},
    midKeys:[],
    netWorkType: "",
    searchMoreHidden:true
  },

  toParkingDetailsTap:function(e){
    var goodsDetailId = e.currentTarget.dataset.id;
     wx.navigateTo({
       url: "/pages/parking-details/index?id="+goodsDetailId 
      })
  },

  scroll: function (e) {
    var that = this,scrollTop=that.data.scrollTop;
    that.setData({
      scrollTop:e.detail.scrollTop
    })
  },
  onLoad: function () {
    var that = this
    WxSearch.init(that, 43, []);
   
  },

  onShow:function(){
    var that = this;
    wx.setNavigationBarTitle({
      title: "好邻居共享停车"
    })

    wx.getNetworkType({
      success: function (res) {
        if (res.networkType == "none" || res.networkType == "unknown") {
          wx.showModal({
          title: '提示',
          content: "请确认打开GPS和网络",
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
            }
          })
        } else {
          that.getGoodsList(0);
        }
      }
    })
  },





//获取
getGoodsList: function (categoryId) {
    wx.showLoading();
    var that = this;
    wx.getLocation({
      type: 'wgs84', //返回可以用于wx.openLocation的经纬度
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        wx.request({
          url: app.globalData.domain+app.globalData.subDomain+ '/index/getNearParkingList?',
          method: 'post',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          data: {
            'openid':_openid,
            'latitude': latitude, //返回的纬度
            'longitude': longitude, //返回的经度
          },
          success: function (res) {
            wx.setStorageSync('wx', res.longitude);
            wx.hideLoading();
            var goods = [];
            if (res.data.code == 100){
              if (res.data.obj.length == 0) {
                that.setData({
                  loadingMoreHidden: false,
                });
                return;
              }
              for (var i = 0; i < res.data.obj.length; i++) {
                goods.push(res.data.obj[i]);
              }
              that.setData({
                goods: goods,
                loadingMoreHidden: true
              });
              console.log(goods);
            }else{
              var tip = "数据获取失败," + res.data.message;
              wx.showModal({
                title: '提示',
                content: tip,
                success: function (res) {
                  if (res.confirm) {
                    console.log('用户点击确定')
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                  }
                }
              })
            }
            for (var i = 0; i < res.data.obj.length; i++) {
              goods.push(res.data.obj[i]);
            }
            that.setData({
              goods: goods,
              loadingMoreHidden: true,
              searchMoreHidden:true
            });
            console.log(goods);
          }
        })
      }
      })
  },


getNearbyParkingList: function (e) {
  console.log("openid:"+ _openid);
  var tempOpenid = wx.getStorageSync("wxOpenid");
  wx.showLoading();
  var that = this;
  var inputStr = e;
  wx.request({
    url: app.globalData.domain + app.globalData.subDomain + '/parking/search?',
    method: 'post',
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    data: {
      'openid': tempOpenid,
      'word':inputStr
    },
    success: function (res) {
      wx.hideLoading();

      var goods= [];
      if(res.code == 100){
        goods = res.data.obj;
        that.setData({
          goods: goods,
          searchMoreHidden:true
        });
      }else{
        that.setData({
          goods: null,
          searchMoreHidden: false
        })
      }
      console.log(goods);
    }
  })
},
///搜索相关功能
listenerSearchInput: function (e) {
  this.setData({
    searchInput: e.detail.value
  })

  if(this.data.searchInput.length == 0){
    this.getGoodsList();
  }else{
    this.getNearbyParkingList(this.data.searchInput);
  }



},
toSearch: function () {
  this.getNearbyParkingList(this.data.searchInput);
}
})
